package com.example.shopping;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.finalapp.R;
import com.example.finalapp.SplitActionBarActivity;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class ShoppingListActivity extends SplitActionBarActivity {

	private static final int DELETE_ID = 0;
	ListView lv;
	public static ArrayList<ShoppingList> ShoppingListArr = new ArrayList<ShoppingList>();
	CustomAdapter Adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("ShoppingList", "onCreate");
		setContentView(R.layout.activity_shopping_list); 

		fillData();


		lv = (ListView)findViewById(R.id.shopping_list);	
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position, long id) { 
				Intent intent = new Intent(ShoppingListActivity.this, ShoppingEditActivity.class);
				startActivity(intent);
			}

		});


		// Now create an array adapter and set it to display using our row
		Adapter= new CustomAdapter(ShoppingListActivity.this, R.layout.notes_row, ShoppingListArr);

		lv.setAdapter(Adapter);

		registerForContextMenu(lv);
		Button addnote = (Button)findViewById(R.id.addnotebutton);

		addnote.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				createNote();   	

			}
		});

	}


	@Override
	protected void onResume() {
		super.onResume();
		Log.d("ShoppingList", "onResume");

		//Adapter.notifyDataSetChanged();
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.shopping_list, menu);

		return true;		
	}

	

	private void createNote() {
		Intent intent = new Intent(this, ShoppingEditActivity.class);
		startActivity(intent);    	
	}


	class CustomAdapter extends ArrayAdapter<ShoppingList>{

		Context context; 
		int layoutResourceId;    
		ArrayList<ShoppingList> data = null;
		private LayoutInflater mInflater; 	

		public CustomAdapter(Context customAdapter, int layoutResourceId, ArrayList<ShoppingList> data) {

			super(customAdapter, layoutResourceId, data);	
			this.layoutResourceId = layoutResourceId;
			this.context = customAdapter;
			this.data = data;
			this.mInflater = LayoutInflater.from(customAdapter);						
		}

		public View getView(final int position, View convertView, ViewGroup parent) {		

			ViewHolder holder = null;		       

			if (convertView == null) {

				//item_list
				convertView = mInflater.inflate(R.layout.notes_row, null);

				holder = new ViewHolder();

				//fill the views
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.date = (TextView) convertView.findViewById(R.id.date_row);

				convertView.setTag(holder);						
			} 
			else {
				// Get the ViewHolder back to get fast access to the TextView
				holder = (ViewHolder) convertView.getTag();//			
			}
			holder.title.setText(data.get(position).title);
			holder.date.setText(data.get(position).date);

			return convertView;
		}


		class ViewHolder {		
			TextView title;	
			TextView date;
		}

	}

	private void fillData() {
		// Get all of the notes from the database and create the item list
		ParseQuery<ParseObject> query = ParseQuery.getQuery("shoppingLists");
		query.whereEqualTo("exists", true);
		query.findInBackground(new FindCallback<ParseObject>() {
			public void done(List<ParseObject> listDB, ParseException e) {
				if (e == null) {
					Log.d("listDB", "Retrieved " + listDB.size() + " size");

					for( int i = 0; i<listDB.size(); i++)
					{
						ShoppingList obj = new ShoppingList(listDB.get(i).getString("title").toString(),listDB.get(i).getString("date").toString());
						ShoppingListArr.add(obj);
					}

				} else {
					Log.d("listDB", "Error: " + e.getMessage());
				}
			}
		});
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		//menu.add(0, DELETE_ID, 0, "menu_delete");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case DELETE_ID:
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
			//mDbHelper.deleteNote(info.id);
			return true;
		}
		return super.onContextItemSelected(item);
	}


}
